package com.campusnews.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.campusnews.mapper.*;
import com.campusnews.pojo.*;
import com.campusnews.service.InvitationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.campusnews.utils.DateUtils;
import com.campusnews.utils.EntryToMapUtils;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lqc
 * @since 2021-09-26
 */
@Service
public class InvitationServiceImpl extends ServiceImpl<InvitationMapper, Invitation> implements InvitationService {

    @Autowired
    private InvitationMapper invitationMapper;

    @Autowired
    private SchoolUserMapper schoolUserMapper;

    @Autowired
    private BuildMapper buildMapper;

    @Autowired
    private TopicMapper topicMapper;

    /**
     * 删除图片还没有写
     * @param invitation
     * @return
     */
    @Override
    @Transactional
    public boolean deleteInvite(Invitation invitation) {
        int number = 0;
        int updateNumber = 0;
        int count = 0;
        number = invitation.getSchoolUser().getInvitationNumber();
        if (number>0){
            number-=1;
            invitation.getSchoolUser().setInvitationNumber(number);
            updateNumber+=schoolUserMapper.updateById(invitation.getSchoolUser());
            count++;
        }

        number = invitation.getBuild().getInviNum();
        if (number>0){
            number-=1;
            invitation.getBuild().setInviNum(number);
            updateNumber+=buildMapper.updateById(invitation.getBuild());
            count++;
        }

        List<Topic> topics = invitation.getTopics();
        for (Topic topic : topics) {
            number = topic.getInviNum();
            if (number>0){
                number-=1;
                topic.setInviNum(number);
                updateNumber+=topicMapper.updateById(topic);
                count++;
            }
        }
        updateNumber+=invitationMapper.deleteById(invitation.getInviId());
        count++;
        if(updateNumber ==count){
            return true;
        }else {
            return false;
        }
    }

//    /**
//     * 图片上传，话题topic_list集合
//     * @param invitation
//     * @return
//     */
//    @Override
//    @Transactional
//    public boolean insertInvite(Invitation invitation) {
//        int number = 0;
//        int updateNumber = 0;
//        int count = 0;
//        number = invitation.getSchoolUser().getInvitationNumber();
//        number+=1;
//        invitation.getSchoolUser().setInvitationNumber(number);
//        updateNumber+=schoolUserMapper.updateById(invitation.getSchoolUser());
//        count++;
//        System.out.println(number);
//
//        number = invitation.getBuild().getInviNum();
//        number+=1;
//        invitation.getBuild().setInviNum(number);
//        invitation.getBuild().setInviId(invitation.getInviId());
//        updateNumber+=buildMapper.updateById(invitation.getBuild());
//        count++;
//        System.out.println(number);
//
//        String topicList = "";
//        List<Topic> topics = invitation.getTopics();
//        for (Topic topic : topics) {
//            number = topic.getInviNum();
//            number+=1;
//            topic.setInviNum(number);
//            topicList=topicList+topic.getTopicId()+",";
//            updateNumber+=topicMapper.updateById(topic);
//            count++;
//            System.out.println(number);
//        }
//        invitation.setTopicList(topicList);
//
//        updateNumber+=invitationMapper.insert(invitation);
//        count++;
//        if(updateNumber ==count){
//            return true;
//        }else {
//            return false;
//        }
//    }
//
    /*
    * 修改是大头
    * */
    @Override
    @Transactional
    public boolean updateInvite(Invitation invitation) {
        Invitation invitationOld = invitationMapper.selectById(invitation);
        int update = invitationMapper.updateById(invitation);
        if (update==1){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public Invitation getInviteInformation(Invitation invitation) {
        SchoolUser user = schoolUserMapper.selectById(invitation.getUserId());
        invitation.setSchoolUser(user);
        Build build = buildMapper.selectById(invitation.getBuildId());
        invitation.setBuild(build);
        String inviPic = invitation.getInviPic();
        List<String> picUrlList =null;
        if (inviPic!=null){
            String[] split = inviPic.split(",");
            picUrlList = Arrays.asList(split);
        }
        invitation.setPicUrlList(picUrlList);

        List<String> topics = null;
        List<Topic> topics2 = null;
        if (invitation.getTopicList()!=null){
            String[] Topicsplit = invitation.getTopicList().split(",");
            topics = Arrays.asList(Topicsplit);
            topics2 = topicMapper.selectBatchIds(topics);
        }
        invitation.setTopics(topics2);
        return invitation;
    }

    @Override
    public PageInfo<Invitation> selectInviteByAccurate(Invitation invitation) {
        Map<String, Object> map = new HashMap<>();
        //实体类转化为map集合
        try {
            map = EntryToMapUtils.entryToMap(invitation);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        //条件查询器
        QueryWrapper<UserLook> wrapper1 = new QueryWrapper<>();
//        wrapper1.eq("user_id",)
//        userLookMapper.selectList()
        QueryWrapper<Invitation> wrapper = new QueryWrapper<>();
        wrapper.allEq(map);
        if (!(invitation.getTopicList()==null||invitation.getTopicList().length()==0)){
            wrapper.like("topic_list",invitation.getTopicList());
        }
        if (!(invitation.getSortOrder()==null||invitation.getSortOrder().length()==0)){
            wrapper.orderByDesc(invitation.getSortOrder());
        }
        PageHelper.startPage(invitation.getPage(),invitation.getPageSize());
        List<Invitation> invitationList = invitationMapper.selectList(wrapper);
        for (int i = 0; i < invitationList.size(); i++) {
            invitationList.set(i,getInviteInformation(invitationList.get(i)));
        }
        PageInfo<Invitation> pageInfo = new PageInfo<>((Page<Invitation>)invitationList);
        return pageInfo;
    }

    @Override
    public PageInfo<Invitation> selectInviteByTopic(Thing things) {
        //条件查询器
        QueryWrapper<Invitation> wrapper = new QueryWrapper<>();
        wrapper.like("topic_list",things.getThing());
        if (!(things.getSortOrder()==null||things.getSortOrder().length()==0)){
            wrapper.orderByDesc(things.getSortOrder());
        }
        PageHelper.startPage(things.getPage(),things.getPageSize());
        List<Invitation> invitationList = invitationMapper.selectList(wrapper);
        for (int i = 0; i < invitationList.size(); i++) {
            invitationList.set(i,getInviteInformation(invitationList.get(i)));
        }
        PageInfo<Invitation> pageInfo = new PageInfo<>((Page<Invitation>)invitationList);
        System.out.println(pageInfo);
        return pageInfo;
    }

    @Override
    public PageInfo<Invitation> selectInviteByVague(Thing things) {
        //条件查询器
        QueryWrapper<Invitation> wrapper = new QueryWrapper<>();
        String thing = things.getThing();
        if (!(thing==null||thing.length()==0)){
            wrapper.like("invi_title",thing).or()
                    .like("invi_content",thing);
        }
        if (!(things.getSortOrder()==null||things.getSortOrder().length()==0)){
            wrapper.orderByDesc(things.getSortOrder());
        }
        PageHelper.startPage(things.getPage(),things.getPageSize());
        List<Invitation> invitationList = invitationMapper.selectList(wrapper);
        for (int i = 0; i < invitationList.size(); i++) {
            invitationList.set(i,getInviteInformation(invitationList.get(i)));
        }
        PageInfo<Invitation> pageInfo = new PageInfo<>((Page<Invitation>)invitationList);
        System.out.println(pageInfo);
        return pageInfo;
    }
}
